'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/mp/kh/getMpQr 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _, $ } = util;
		let { uid, page, scene } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
		// 获取bid
		let business = await vk.daoCenter.businessDao.findById("6780d398f019be8d7a5f5757")
		
		if (!business || Object.keys(business).length <= 0) {
			return {
				code: -1,
				msg: "错误数据，请联系开发者"
			};
		}
		
		// 生成二维码
		
		let customConfig = {
			
		}
		customConfig.appid = business.mp.appid;
		customConfig.appsecret = business.mp.appsecret;
		let getUnlimitedRes = await vk.openapi.weixin.wxacode.getUnlimited({
			page,
			scene,
			check_path: false,
			env_version: "develop", // 要打开的小程序版本。正式版为 "release"，体验版为 "trial"，开发版为 "develop"。默认是正式版。
			...customConfig
		});
			
		
		if (typeof getUnlimitedRes === "object" && getUnlimitedRes.code) {
			return getUnlimitedRes;
		}
		try {
			let base64 = Buffer.from(getUnlimitedRes, 'binary').toString('base64')
			return {
				code: 0,
				base64: `data:image/png;base64,${base64}`
			};
		} catch (err) {
			// 转base64失败
			return {
				code: -1,
				msg: "生成小程序码失败",
				err: {
					message: err.message,
					stack: err.stack
				}
			};
		}
	}
}